// pages/shouye/duihuan/duihuan.js
const app = getApp()
import {
  HomeIndex
} from 'index-modal.js';
import {
  Config
} from '../../../utils/config.js';
var Function = require("../../../utils/function.js");
var index = new HomeIndex(); //实例化 首页 对象


Page({

  /**
   * 页面的初始数据
   */
  data: {
    txtVal: '',
    code: '' //兑换码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this._exchange()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },


  _exchange() {
    let that = this;
    let data = {
      code: that.data.code,
      uid: wx.getStorageSync('members').id
    }
    index.exchange(data, (res) => {
      if (that.data.code == '') {
        that.setData({
          txtVal: res.data.coupon_exchange
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1000
        })
      }
    })
  },
  likeHui() {
    let token = wx.getStorageSync('members').token || '';
    if (token != '') {
      this.data.code != "" ? this._exchange() : wx.showToast({
        title: '不能为空',
        duration: 1000,
        icon: 'none'
      });
    } else {
      wx.navigateTo({
        url: '/pages/auth/auth',
      })
    }
  },
  bindcode(e) {
    this.setData({
      code: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})